<?php
namespace Home\Common;
use Org\Util\Mysql;

class MysqlOps extends Mysql{
	//信息写入
	function insertItem($tablename,$insert_){
		$keys = ""; $values = "";
		foreach($insert_ as $key=>$value){
			$keys .=$key.","; $values .= $value.",";
		}
		$keys = substr($keys,0,strlen($keys)-1);
		$values = substr($values,0,strlen($values)-1);
		
		$insert = $this->mysql->prepare("insert into $tablename($keys) values ($values)");
		$status = $insert->execute();
		
		//return "insert into $tablename($keys) values ($values)";
		return $status;
	}
	//信息获取
	function selectItem($tablename,$where_ = null,$keys_ = null,$limit_ = null){
		if(isset($where_)){
			$where = "";
			foreach($where_ as $key => $value){
				$where .= $key." = ".$value." and ";
			}
			$where = substr($where, 0 ,strlen($where)-4);
			$where = "where ".$where;
		}else{
			$where = "";
		}
		
		if(isset($keys_)){
			$keys = "";
			foreach($keys_ as $value){
				$keys .= $value.",";
			}
			$keys = substr($keys, 0, strlen($keys)-1);
		}else{
			$keys="*";
		}
		
		if(isset($limit_)){
			$limit = "limit $limit_[0],$limit_[1]";
		}else{
			$limit = "";
		}
		
		$select = $this->mysql->prepare("select $keys from $tablename $where $limit");
		$select->execute();
		
		$arResult = array();
		while($row = $select->fetch(\PDO::FETCH_ASSOC)){
			array_push($arResult, $row);
		}
		
		return $arResult;
	}
	//更改信息
	function updateItem($tablename,$set_,$where_){
		//set字符串格式化
		$set = "set ";
		foreach($set_ as $key => $value){
			$set .= $key." = ".$value." ,";
		}
		$set = substr($set,0,strlen($set)-1);
		
		//where字符串格式化
		$where = "where ";
		foreach($where_ as $key => $value){
			$where .= $key." = ".$value." and ";
		}
		$where = substr($where, 0 ,strlen($where)-4);
		
		$update = $this->mysql->prepare("update $tablename $set $where");
		$status = $update->execute();
		
		return $status;
	}
	
	//获取最大ID
	function getMaxId(){
		$select = $this->mysql->prepare("select max(workerid) workerid from iry_worker_info");
		$select->execute();
		
		$row=$select->fetch(\PDO::FETCH_ASSOC);
		return $row['workerid'];
	}
	
	/*获取count数量*/
	 function getCount($tablename,$where_){
	 	//where字符串格式化
	 	$where = "where ";
	 	foreach($where_ as $key=>$value){
	 		$where .= $key." = ".$value." and";
	 	}
	 	$where = substr($where,0,strlen($where)-4);
	 	
	 	$select = $this->mysql("select count(*) from $tablename $where");
	 	$select->execute();
	 	
	 	$row = $select->fetch(\PDO::FETCH_ASSOC);
	 	return $row['coun(*)'];
	 }
}

